import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {getInfo, logout} from '@/api/login'
import {getToken, removeToken} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: storage.get(constant.name),
    nickName: storage.get(constant.nickName),
    userId: storage.get(constant.userId),
    phoneNumber: storage.get(constant.phoneNumber),
    avatar: storage.get(constant.avatar),
    roles: storage.get(constant.roles),
    permissions: storage.get(constant.permissions)
  },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
            uni.setStorageSync('refreshToken',token)
        },
        SET_NAME: (state, name) => {
            state.name = name
            storage.set(constant.name, name)
        },
        SET_NICKNAME: (state, nickName) => {
          state.nickName = nickName
          storage.set(constant.nickName, nickName)
        },
        SET_USERID: (state, userId) => {
          state.userId = userId
          storage.set(constant.userId, userId)
        },
        SET_PHONE_NUMBER: (state, phoneNumber) => {
            state.phoneNumber = phoneNumber
            storage.set(constant.phoneNumber, phoneNumber)
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
            storage.set(constant.avatar, avatar)
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
            storage.set(constant.roles, roles)
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
            storage.set(constant.permissions, permissions)
        }
    },
   

  actions: {
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = (user == null || user.avatar === "" || user.avatar == null) ? '' : user.avatar
          const username = (user == null || user.userName === "" || user.userName == null) ? "" : user.userName
          const phoneNumber = (user == null || user.phonenumber === "" || user.phonenumber == null) ? "" : user.phonenumber
          const userId = (user == null || user.userId === "" || user.userId == null) ? "" : user.userId
          const nickName = (user == null || user.nickName === "" || user.nickName == null) ? "" : user.nickName
          const userType = (user == null || user.userType === "" || user.userType == null) ? "" : user.userType
          if (res.roles && res.roles.length > 0) {
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', username)
          commit('SET_NICKNAME', nickName)
          commit('SET_USERID', userId)
          commit('SET_AVATAR', avatar)
          commit('SET_PHONE_NUMBER', phoneNumber)
          uni.setStorageSync('userType',userType)
          console.log(uni.getStorageSync('userType'));
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          // commit('SET_ROLES', [])
          // commit('SET_PERMISSIONS', [])
          // commit('SET_NAME', '')
          // commit('SET_AVATAR', '')
          // commit('SET_USERID', '')
          // commit('SET_NICKNAME', '')
          // commit('SET_PHONE_NUMBER', '')
          removeToken()
          storage.clean() 
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
